using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Jbe.TestSuite.Infrastructure.Interface;
using System.Windows.Forms;
using Jbe.TestSuite.TestDevice.Manager;
using Microsoft.Practices.CompositeUI.UIElements;
using Jbe.TestSuite.TestDevice.Manager.Constants;
using Microsoft.Practices.CompositeUI.Commands;
using Jbe.TestSuite.TestDevice.Manager.Views;
using Microsoft.Practices.CompositeUI.EventBroker;
using Jbe.TestSuite.TestDevice.Manager.Interface.Services;
using Jbe.TestSuite.TestDevice.Manager.Tests.Mocks;

namespace Jbe.TestSuite.TestDevice.Manager.Tests.Views
{
    /// <summary>
    /// Summary description for TestDeviceViewPresenterFixture
    /// </summary>
    [TestClass]
    public class TestDevicesViewPresenterFixture : FixtureBase
    {
        public event EventHandler<EventArgs<IList<IControlView>>> DeviceControllersChanged;
        
        private ControlledWorkItem<ModuleController> workItem;
        private TestDevicesViewPresenter presenter;
        private MockTestDevicesView mockView;
        private IList<IControlView> selectedDeviceControllers;

        [TestInitialize]
        public override void TestInitialize()
        {
            base.TestInitialize();

            workItem = rootWorkItem.WorkItems.AddNew<ControlledWorkItem<ModuleController>>();
            workItem.Controller.Run();

            presenter = new TestDevicesViewPresenter();
            mockView = workItem.SmartParts.AddNew<MockTestDevicesView>();
        }

        [TestMethod]
        public void TestDeviceControllerChanged()
        {
            workItem.EventTopics[EventTopicNames.ControlViewsChanged]
                .AddPublication(this, "DeviceControllersChanged", rootWorkItem, PublicationScope.Global);

            List<IControlView> deviceControllers = new List<IControlView>();
            DeviceControllersChanged(this, new EventArgs<IList<IControlView>>(deviceControllers));

            Assert.AreEqual(deviceControllers, mockView.DeviceControllers, "DeviceControllers");
        }

        [TestMethod]
        public void ChangeSelectedDeviceControllers()
        {
            workItem.EventTopics[EventTopicNames.SelectedControlViewsChanged]
                .AddSubscription(this, "SelectedDeviceControllersChangedHandler", workItem, ThreadOption.Publisher);

            List<IControlView> select = new List<IControlView>();
            mockView.SetSelectedDeviceControllers(select);

            Assert.AreEqual(select, selectedDeviceControllers, "SelectedDeviceControllers");
        }

        public void SelectedDeviceControllersChangedHandler(object sender, EventArgs<IList<IControlView>> e)
        {
            selectedDeviceControllers = e.Data;
        }
    }
}
